//
//  StatusCell.swift
//  Weibo
//
//  Created by 张婷 on 15/10/26.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit
/// 微博 Cell 控件间距
let StatusCellMargin: CGFloat = 12
/// 微博 Cell 头像尺寸
let StatusIconWidth: CGFloat = 35

/// 微博 cell
class StatusCell: UITableViewCell {
    
    /// 微博视图模型
    var viewModel: StatusViewModel? {
        didSet {
            topView.viewModel = viewModel
            contentLabelView.text = viewModel?.statusText
        
            // 配图视图
            pictureView.viewModel = viewModel
            pictureView.snp_updateConstraints { (make) -> Void in
                make.height.equalTo(pictureView.bounds.height)
                make.width.equalTo(pictureView.bounds.width)
            }
        }
    }
    
    ///  根据视图模型计算行高
    ///
    ///  - parameter vm: 视图模型
    ///
    ///  - returns: 行高
    func rowHeight (vm: StatusViewModel) -> CGFloat {
        
        // 1. 设置视图模型
        viewModel = vm
        
        // 2. 更新约束
        layoutIfNeeded()
        
        // 3. 返回底部视图最大高度
        return CGRectGetMaxY(bottomView.frame)
    }
    
    /// MARK: - 构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// MARK: - 懒加载控件
    // 头部视图
    private lazy var topView: StatusCellTopView = StatusCellTopView()
    // 中间正文
    private lazy var contentLabelView: UILabel = UILabel(
        title: "微博正文",
        color:  UIColor.darkGrayColor(),
        sizeFont: 15,
        screenInset: StatusCellMargin)
    // 图片展示视图
    private lazy var pictureView: StatusCellPictureView = StatusCellPictureView()
    // 底部视图
    private lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
}

// MARK: - 页面设置
extension StatusCell {
    
    private func setupUI() {
        
        // 添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabelView)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        // 分割线视图
        let sepView = UIView()
        sepView.backgroundColor = UIColor.lightGrayColor()
        contentView.addSubview(sepView)
        
        // 自动布局
        sepView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(StatusCellMargin)
        }
        // 1> 顶部视图
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(2 * StatusCellMargin + StatusIconWidth)
        }
        // 2> 内容标签
        contentLabelView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom)
            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)
        }
        // 3> 视图配图
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabelView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)
            make.height.equalTo(180)
            make.width.equalTo(300)

        }
        // 4> 底部视图
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
            
           // make.bottom.equalTo(contentView.snp_bottom)
        }

    }
}
